package cn.com.scitc.dao;

import org.junit.jupiter.api.Test;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnDB {

    public static Connection conn;

    public static Connection openConn() {
        Properties proper = new Properties();
        try {
            proper.load(new FileReader("src/conn.properties"));

            String driver = proper.getProperty("driver");
            String url = proper.getProperty("url");
            String user = proper.getProperty("user");
            String passwd = proper.getProperty("passwd");

            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, passwd);
            System.out.println("数据库连接成功！");
            return conn;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeConn(){
        if (conn != null) {
            try {
                System.out.println("数据库关闭成功！");
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
